package com.gem.util;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class XMLHelper {
	public static Map readXML(){
		Map<String,String> map=new HashMap<String,String>();
		Document doc=null;
		String driver_class=null;
		String password=null;
		String url=null;
		String username=null;
		SAXReader reader=null;
		try{
			reader=new SAXReader();
			doc=reader.read(new File("d:\\database.cfg.xml"));
			Element rootEle=doc.getRootElement();
			Iterator iter = rootEle.elementIterator("database"); 
			if(iter.hasNext()){
				Element itemEle = (Element) iter.next();
				map=new HashMap<String,String>();
				driver_class=itemEle.elementTextTrim("connection.driver_class");
				password=itemEle.elementTextTrim("connection.password");
				url=itemEle.elementTextTrim("connection.url");
				username=itemEle.elementTextTrim("connection.username");
				map.put("driver_class", driver_class);
				map.put("password", password);
				map.put("url", url);
				map.put("username", username);
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		return map;
	}
}
